package chapter12

/**
  * @Copyright: Shanghai Definesys Company.All rights reserved.
  * @Description:
  * @author: chuhaitao
  * @since: 2019/1/14 22:13
  * @history:
  *          1.2019/1/14 created by chuhaitao
  */
object FoldDemo {

  def main(args: Array[String]): Unit = {
    var list01 = List(1, 23, 123, 12, 6, 12, 8)

    //（（（（（5-1）-23 ）-123）-12）-6）-12）-8
    //类似于List(5，1,23,123,12,6,12,8).reducerLeft(f*)
    var res1 = list01.foldLeft(5)((n1: Int, n2: Int) => {
      println(n1 - n2)
      n1 - n2
    })

    println(res1)



    //扫描保留了每一次计算的结果
    //List(5, 4, -19, -142, -154, -160, -172, -180)

    var res03 = list01.scanLeft(5)((n1: Int, n2: Int) => {
      n1 - n2
    })
    println(res03)

  }

}
